﻿using ConfExpert.Forms.Base;
using ConfExpert.Utility;
using DevExpress.XtraGrid.Columns;
using DevTeam;
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using System.Linq;

namespace ConfExpert.Forms.Espresso
{
    public partial class PerfArtByCustomer : XForm
    {
        public PerfArtByCustomer()
        {
            InitializeComponent();
        }

        private void PerfArtByCustomer_Load(object sender, EventArgs e)
        {
            gridView1.Init("GetPerfArtByCustomer", cmbYil.EditValue.ToInt());
            Adj();

            cmbYil.EditValueChanged += cmbYil_EditValueChanged;
        }

        void cmbYil_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                gridView1.RefreshDataSource("GetPerfArtByCustomer", cmbYil.EditValue.ToInt());
                Adj();

            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void Adj()
        {
            //string[] custarray = new string[10] { "149", "75", "269", "4", "323", "21", "68", "96", "102", "241" };
            Dictionary<int, int> dict = new Dictionary<int, int>();
            int sira = 0;
            foreach (DataRow r in gridView1.Dataset().Tables[1].Rows)            
                dict.Add(r["CikilanCustomerId"].ToInt(), ++sira);            

            foreach (GridColumn col in gridView1.Columns)
                if (col.ColumnType == typeof(decimal))
                {
                    gridView1.FormatAsNumber(1, col.FieldName);
                    gridView1.AddSummary(col.FieldName);
                    if (col.FieldName.ToInt() > 0 &&
                        col.FieldName.ToInt() < 9999)
                        col.Caption = (dict[col.FieldName.ToInt()].ToStr() + ". " + AppSession.Lookups.V_Customer.FindByCustomerId(col.FieldName.ToInt()).Name);
                }



            string[] arr = dict
  .Select(kvp => kvp.Key.ToString()/* + ", " + kvp.Value.ToString()*/)
  .ToArray();

            gridView1.YanYanaGetirSoluna("9999", arr);
            gridView1.SetCaptions("9999:Genel Toplam");
            gridView1.BestFitColumns();

            foreach (GridColumn col in gridView1.Columns)
                if (col.ColumnType == typeof(decimal))
                    col.Width = 75;
        }
    }
}
